<template>
    <div class="CheckContainer">
        <div class="lfCon1">
            <el-checkbox v-model="itemData.isCommonCheck"  class="lfCon1-item" :disabled="props.isRead">通用</el-checkbox>
            <dept-common 
                pageSrc="checkConfig"
                :isRead="props.isRead" 
                :isCheck="itemData.isCommonCheck" 
                :itemCheck="itemData.commonCheck" 
                :options="options1" 
                @changeCheck="changeCheck1" 
                @saveRiskBusiness="saveRiskBusiness1"
                @changeSpeCheckType="changeSpeCheckType1"
                @saveClassName="saveClassName1"
            />
        </div>
        <div class="lfCon1">
            <div>
                <el-checkbox v-model="itemData.isDirectCheck" class="lfCon1-item" :disabled="props.isRead">指定部门</el-checkbox>
                <ul style="margin-left: 22px;" v-if="itemData.isDirectCheck">
                    <li v-for="(item,index) in deptList" :key="index">
                        <el-checkbox v-model="item.isCheck" class="lfCon1-item" :disabled="props.isRead">{{ item.name }}</el-checkbox>
                        <dept-common 
                            pageSrc="checkConfig"
                            :isRead="props.isRead" 
                            :isCheck="item.isCheck" 
                            :itemCheck="item.itemCheck" 
                            :options="options1" 
                            :index="index"
                            @changeCheck="changeCheck2" 
                            @saveRiskBusiness="saveRiskBusiness2"
                            @changeSpeCheckType="changeSpeCheckType2"
                            @saveClassName="saveClassName2"
                        />
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
// 引入api
import type { NodeDataType } from './index.vue';
import useDictStore from '@/store/modules/dict';
import DeptCommon from './checkDeptCommon.vue';

const props = defineProps({
    isRead: {
        type: Boolean,
        default: false,
    }
})
let dictStore = useDictStore();
const nodeData = inject('nodeData', ref<NodeDataType>({
    id: null,
    type: null,
    typeName: null,
    code: null,
    name: null,
    position: { x: 0, y: 0 },
}));
const itemData = ref<any>({});
// 审核类型
const changeSpeCheckType1 = (msg: any)=>{
    itemData.value.commonCheck.dictSpeCheckType = msg.val;
}
const changeSpeCheckType2 = (msg: any)=>{
    deptList.value[msg.index].itemCheck.dictSpeCheckType = msg.val;
}
const saveRiskBusiness1 = (msg:any)=>{
    itemData.value.commonCheck.forRiskBusinessId = msg.val.id; 
    itemData.value.commonCheck.riskBusinessName = msg.val.name;   
}
const saveRiskBusiness2 = (msg:any)=>{
    deptList.value[msg.index].itemCheck.forRiskBusinessId = msg.val.id; 
    deptList.value[msg.index].itemCheck.riskBusinessName = msg.val.name;   
}
const saveClassName1 = (msg:any)=>{
    itemData.value.commonCheck.className = msg.val;
    // console.log('方法：',msg.val);
}
const saveClassName2 = (msg:any)=>{
    deptList.value[msg.index].itemCheck.className = msg.val;
}
// 审核人
const changeCheck1 = (msg:any)=>{   
    itemData.value.commonCheck.dictSpeCheckor = msg.val;
    itemData.value.commonCheck.forRiskBusinessId = null;
    itemData.value.commonCheck.riskBusinessName = null;
    itemData.value.commonCheck.className = '';
}
const changeCheck2 = (msg: any)=>{
    deptList.value[msg.index].itemCheck.dictSpeCheckor = msg.val;
    deptList.value[msg.index].itemCheck.forRiskBusinessId = null;
    deptList.value[msg.index].itemCheck.riskBusinessName = null;
    deptList.value[msg.index].itemCheck.className = '';
}

const options1 = ref<any[]>([]);
const deptList = ref<any[]>([]);

const initializeData = () => {
    try {
        itemData.value = nodeData.value.businessData;
        options1.value = dictStore.dicts.SPE_CHECK_TYPE.list;

        const deptListOld = dictStore.dicts.DEPT_CFG_CATEGORY.list;
        if (deptListOld && deptListOld.length > 0) {
            deptList.value = deptListOld
                .filter((item: any) => parseInt(item.code) > 0)
                .map((item: any) => ({
                    name: item.name,
                    code: item.code,
                    isCheck: false,
                    itemCheck: {
                        dictSpeCheckType: '1',
                        forRiskBusinessId: '',
                        riskBusinessName: '',
                        className: '',
                    }
                }));

            if (itemData.value.directChecks && itemData.value.directChecks.length > 0) {
                itemData.value.directChecks.forEach((item: any) => {
                    const matchedDept = deptList.value.find(dept => dept.code === item.dictDeptCfgCategory);
                    if (matchedDept) {
                        matchedDept.isCheck = true;
                        matchedDept.itemCheck = {
                            ...matchedDept.itemCheck,
                            ...item
                        };
                    }
                });
            }
        }
    } catch (error) {
        console.error('Error initializing data:', error);
    }
}
onMounted(() => {
    initializeData()
})

watch(nodeData, () => {
    initializeData();
}, { deep: true });

const isInfoPass = ref(true);
const allNotChecked = ref(true);

const doCheck = () => {
    isInfoPass.value = true;
    if(itemData.value.isCommonCheck){
        if( !itemData.value.commonCheck.forRiskBusinessId && 
            !itemData.value.commonCheck.className
        ){
            isInfoPass.value = false;
            ElMessage.error('启动校验-通用存在未完善的信息！');
            return
        }
    }
    if(itemData.value.isDirectCheck){
        allNotChecked.value = true; // 假设所有部门都没有被选中
        for (let i = 0; i < deptList.value.length; i++) {
            if (deptList.value[i].isCheck) {
                allNotChecked.value = false; // 如果有一个部门被选中，则设置为false
                if (!deptList.value[i].itemCheck.forRiskBusinessId && 
                    !deptList.value[i].itemCheck.className
                ) {
                    isInfoPass.value = false;
                    break;
                }
            }
        }
        if (allNotChecked.value || !isInfoPass.value) {
            ElMessage.error('启动校验-指定部门存在未完善的信息！');
            return
        }
    }
}
const doSave = () => {
    itemData.value.directChecks = deptList.value
        .filter(item => item.isCheck)
        .map(item => ({
            dictDeptCfgCategory: item.code,
            dictSpeCheckType: item.itemCheck.dictSpeCheckType,
            forRiskBusinessId: item.itemCheck.forRiskBusinessId,
            riskBusinessName: item.itemCheck.riskBusinessName,
            className: item.itemCheck.className,
        }));
}
defineExpose({
    isInfoPass, allNotChecked, doCheck, doSave, itemData
});
</script>
<style scoped lang="scss">
.CheckContainer {
    margin: 10px 10px 0;
}
</style>
